//
//  StatMenuScene.m
//  GameProject
//
//  Created by Blair Trusler on 23/08/2011.
//  Copyright 2011 Blair Trusler. All rights reserved.
//

#import "GameHeader.h"

@implementation StatMenuScene


#pragma mark - scene
+(id) scene
{
    CCScene *scene = [CCScene node];
    
    StatMenuScene *layer = [StatMenuScene node];
    
    [scene addChild: layer];
    
    return scene;
}


#pragma mark - initialize
-(id) init
{
    if( (self=[super init] )) {
        CCLOG(@"Loading Stat Menu...");
                
        [self loadScreenContents];
        
        CCLOG(@"Stat Menu Loaded.");
    }
    return self;
}

-(void)loadScreenContents{
    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 

    CCLayer *menuLayer = [[CCLayer alloc] init];
    [self addChild:menuLayer];
    
    CCSprite *title = [CCSprite spriteWithFile:@"Pause.png"];
//    CCLabelTTF *title = [CCLabelTTF labelWithString:@"Pause Menu" fontName:@"Courier" fontSize:64];
    CCMenuItemImage *startButton = [CCMenuItemImage itemFromNormalImage:@"backbutton1.png" selectedImage:@"backbutton1.png" target:self selector:@selector(resumeGame:)];
    
    //Audio Toggle Buttons:
    CCMenuItem *unMuteItem, *muteItem;
    CCMenuItemToggle *soundToggleItem;
    unMuteItem = [CCMenuItemImage itemFromNormalImage:@"unmute_button.png" selectedImage:@"unmute_button.png" target:nil selector:nil];
    muteItem = [CCMenuItemImage itemFromNormalImage:@"mute_button1.png" selectedImage:@"mute_button1.png" target:nil selector:nil];
    if (soundToggle == TRUE){
        soundToggleItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(muteAction:)items:muteItem, unMuteItem, nil];
    }
    else if (soundToggle == FALSE){
        soundToggleItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(muteAction:)items:unMuteItem, muteItem, nil];
    }
    
    CCMenu *soundMenu = [CCMenu menuWithItems:soundToggleItem, nil];
    
    CCMenuItemImage *returnToMenuButton = [CCMenuItemImage itemFromNormalImage:@"ReturnToMenu.png" selectedImage:@"ReturnToMenu1.png" target:self selector:@selector(returnMenu:)];
    CCMenuItemImage *retryLevelButton = [CCMenuItemImage itemFromNormalImage:@"RetryLevel.png" selectedImage:@"RetryLevel1.png" target:self selector:@selector(retryLevel:)];
    
    CCMenu *returnMenu = [CCMenu menuWithItems:returnToMenuButton, nil];
    CCMenu *retryMenu =[CCMenu menuWithItems:retryLevelButton, nil];

    CCMenu *menu = [CCMenu menuWithItems: startButton, nil];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        title.position =  ccp(500, 700);
        soundToggleItem.scale = 0.5f;
        soundMenu.position = ccp(950,100);
        startButton.scale = 0.6f;
        menu.position = ccp(975,720);
        returnMenu.position = ccp(500,400);
        retryMenu.position = ccp(500,250);
    }
    else{
        title.position =  ccp(screenSize.width*0.5,screenSize.height*0.9);
        title.scale = 0.5f;
        startButton.scale = 0.4f;
        menu.position = ccp(screenSize.width*0.92,screenSize.height*0.9);

        returnToMenuButton.scale = 0.5f;
        
        retryLevelButton.scale = 0.5f;
        returnToMenuButton.position = ccp(screenSize.width*0.0f,30);
        retryLevelButton.position = ccp(screenSize.width*0.0f,-30);

        soundMenu.position = ccp(screenSize.width*0.95,screenSize.height*0.1);
        soundToggleItem.scale = 0.4f;
            
//        menu.position = ccp(screenSize.width*0.8,screenSize.height*0.78);
//        menu.scale =0.75f;
    }
    
    
    if (levelPlayed == 0){
        [menuLayer addChild:returnMenu];
        
    }
    else{
        [menuLayer addChild:returnMenu];
        [menuLayer addChild:retryMenu];
        
    }
    
    [self addChild: title];

    [menuLayer addChild: menu];
    [menuLayer addChild:soundMenu];
}

-(void)mute:(id)sender{
    CCLOG(@"Mute Audio");
    [[SimpleAudioEngine sharedEngine] setMute:1];
    soundToggle=TRUE;

}
-(void)unmute:(id)sender{
    CCLOG(@"Enable Audio");
    [[SimpleAudioEngine sharedEngine] setMute:0];
    soundToggle=FALSE;

}

-(void)muteAction: (id) sender{
    if (soundToggle == FALSE){
        CCLOG(@"Mute Audio");
        [[SimpleAudioEngine sharedEngine] setMute:1];
        soundToggle=TRUE;
    }
    
    else if (soundToggle == TRUE){
        CCLOG(@"Enable Audio");
        [[SimpleAudioEngine sharedEngine] setMute:0];
        soundToggle=FALSE;
    }
}


#pragma mark - buttons
//Resumes from previous scene!! :D (ie remembers player position)
- (void) resumeGame: (id) sender{
    CCLOG(@"Returning to previous scene.");
    [AudioManager playMenuButtonClick];
    [[CCDirector sharedDirector] resume];
    [[CCDirector sharedDirector] popScene]; //'pops' back to previous scene    
}

-(void)retryLevel:(id) sender{
    [AudioManager playMenuButtonClick];
    [[CCDirector sharedDirector] resume];
    if (levelPlayed==1){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel1];    
    }
    if (levelPlayed==2){
//       CCScene *reloadScene = [Level2Scene node];
//
//        [[CCDirector sharedDirector] replaceScene:reloadScene];
//        [[[CCDirector sharedDirector] runningScene] stopAllActions];
//        [[[CCDirector sharedDirector] runningScene] removeAllChildrenWithCleanup:YES];

        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel2];    

    }
    if (levelPlayed== 3){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel3];    
    }
    if (levelPlayed == 4){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel4];    
    }
    if (levelPlayed == 5){
        [[SceneManager sharedSceneManager] runSceneWithID:kGameLevel5];    
    }
}

-(void)returnMenu:(id) sender{
    if (levelPlayed == 0){
        [AudioManager playMenuButtonClick];
        [[CCDirector sharedDirector] resume];
        [[CCDirector sharedDirector] popScene]; //'pops' back to previous scene    
    }
    else{
        [AudioManager playMenuButtonClick];
        [[CCDirector sharedDirector] resume];
        [[SceneManager sharedSceneManager] runSceneWithID:kMainMenuScene];    
        
    }
}


#pragma mark - dealloc
- (void) dealloc {
    [expLabel dealloc];
    [playerLevelNumber dealloc];
    [super dealloc];
}

@end
